#include "drawlinkitem.h"


DrawLinkItem::DrawLinkItem(QObject *parent): QObject(parent)
{
    setFlag(QGraphicsPathItem::ItemIsSelectable);
    setAcceptHoverEvents(true);
//    setAcceptTouchEvents(true);
    m_bStart = true;
}

void DrawLinkItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setBrush(QColor(255, 255, 0, 100));
    painter->setPen(QPen(Qt::red, 0.1));


    qDebug() << "DrawLinkItem Begin Paint";

    //关键在合理调整连接点算法
    QPolygonF polygon = drawLinkAlogrithum(painter);
    qDebug() << "DrawLinkItem End Paint";

    this->setPolygon(polygon);
}

QPolygonF DrawLinkItem::drawLinkAlogrithum(QPainter *painter)
{
    QPointF qPointA1 = m_qRecFBegin.topRight();
    QPointF qPointA2 = m_qRecFEnd.topLeft();

    QPointF qPointB1 = m_qRecFBegin.bottomRight();
    QPointF qPointB2 = m_qRecFEnd.bottomLeft();

    QPainterPath qPainterPath;

    QPolygonF polygon;
    polygon << qPointA1 << qPointA2 << qPointB2 << qPointB1;
    qPainterPath.addPolygon(polygon);

    painter->drawPath(qPainterPath);

    return polygon;
}

void DrawLinkItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

}

void DrawLinkItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{

}

void DrawLinkItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{

}

void DrawLinkItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{

}

void DrawLinkItem::setRect(QRectF qRectf)
{
    if(m_bStart)
    {
        m_bStart =false;
        m_qRecFBegin = qRectf;
    }
    else
    {
        m_qRecFEnd = qRectf;
    }
}
